package cn.tedu.boot2.controller;

import cn.tedu.boot2.entity.Product;
import cn.tedu.boot2.utils.DBUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@RestController
public class ProductController {
    @RequestMapping("/insert")
    public String insert(Product product){
        System.out.println("product = " + product);
        try (Connection conn = DBUtils.getConn()){
            String sql = "insert into product values(null,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, product.getTitle());
            ps.setInt(2,product.getNum());
            ps.setDouble(3,product.getPrice());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "添加完成!<a href='/'>返回首页</a>";
    }
    @RequestMapping("/select")
    public String select(Product product){
        // 创建集合用于装查询到的商品数据
        ArrayList<Product> list = new ArrayList<>();
        try (Connection conn = DBUtils.getConn()){
            String sql = "select * from product";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            // 遍历结果集对象
            while (rs.next()){
                Product p = new Product();// 实例化对象 用来封装查询到的数据
                p.setId(rs.getInt(1));
                p.setTitle(rs.getString(2));
                p.setNum(rs.getInt(3));
                p.setPrice(rs.getDouble(4));
                list.add(p);// 把创建的对象装进list集合
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // 把集合中的数据装进表格标签最后一起返回给客户端
        String html = "<table border='1'>";
        html+="<tr><th>id</th><th>标题</th><th>库存</th><th>价格</th><th>操作</th></tr>";
        // 遍历商品集合
        for (Product p: list) {
            html+="<tr>";// += 字符串拼接
            html+="<td>"+p.getId()+"</td>";
            html+="<td>"+p.getTitle()+"</td>";
            html+="<td>"+p.getNum()+"</td>";
            html+="<td>"+p.getPrice()+"</td>";
            html+="<td><a href='/delete?id="+p.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }
    @RequestMapping("/delete")
    public String delete(int id){
        System.out.println("id = " + id);
        try (Connection conn = DBUtils.getConn()){
            String sql = "delete from product where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "删除成功!<a href='/select'>返回列表页面</a>";
    }
    @RequestMapping("/update")
    public String update(Product product){
        System.out.println("product = " + product);
        try (Connection conn = DBUtils.getConn()){
            String sql = "update product set title=?,num=?,price=? where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, product.getTitle());
            ps.setInt(2,product.getNum());
            ps.setDouble(3,product.getPrice());
            ps.setInt(4,product.getId());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "修改成功!<a href='/select'>返回列表页面</a>";
    }
}
